package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.DstChannelCfgMapper;
import cc.rengu.oltp.service.common.entity.DstChannelCfg;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 支付通道配置操作表操作接口实现类
 * Created by 王成 on 2020/2/10
 */
public class DstChannelCfgMapperImpl implements DstChannelCfgMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public DstChannelCfgMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertDstChannelCfg(DstChannelCfg dstChannelCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertDstChannelCfg");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, dstChannelCfg, null);
        if (0 != iReturnCode) {
            rglog.error("登记支付通道配置失败,instId:<{}>,dstChannelId:<{}>,dstChannelType:<{}>,iReturnCode:<{}>", dstChannelCfg.getInstId(), dstChannelCfg.getDstChannelId(), dstChannelCfg.getDstChannelType(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateDstChannelCfgByPrimaryKey(DstChannelCfg dstChannelCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateDstChannelCfgByPrimaryKey");
        dstChannelCfg.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, dstChannelCfg, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新支付通道配置失败,instId:<{}>,dstChannelId:<{}>,dstChannelType:<{}>,iReturnCode:<{}>", dstChannelCfg.getInstId(), dstChannelCfg.getDstChannelId(), dstChannelCfg.getDstChannelType(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public DstChannelCfg selectDstChannelCfgByPrimaryKey(String instId, String dstChannelId, String dstChannelType) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectDstChannelCfgByPrimaryKey");
        DstChannelCfg dstChannelCfg = new DstChannelCfg();
        dstChannelCfg.setInstId(instId);
        dstChannelCfg.setDstChannelId(dstChannelId);
        dstChannelCfg.setDstChannelType(dstChannelType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, dstChannelCfg, dstChannelCfg);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询支付通道配置失败,instId:<{}>,dstChannelId:<{}>,dstChannelType:<{}>,iReturnCode:<{}>", instId, dstChannelId, dstChannelType, iReturnCode);
            return null;
        }
        return dstChannelCfg;
    }

    @Override
    public List<DstChannelCfg> selectDstChannelCfgBydstChannelId(String instId, String dstChannelId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectDstChannelCfgBydstChannelId");
        DstChannelCfg dstChannelCfg = new DstChannelCfg();
        dstChannelCfg.setInstId(instId);
        dstChannelCfg.setDstChannelId(dstChannelId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, dstChannelCfg, DstChannelCfg.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据通道标识查询支付通道配置失败,instId:<{}>,dstChannelId:<{}>", instId, dstChannelId);
            return null;
        }
        return objectList.stream().map(item -> (DstChannelCfg) (item)).collect(Collectors.toList());
    }

    @Override
    public List<DstChannelCfg> loadAllDstChannelCfg() throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "loadAllDstChannelCfg");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, null, DstChannelCfg.class);
        if (objectList.isEmpty()) {
            rglog.warn("获取全量通道配置信息失败!");
            return null;
        }
        return objectList.stream().map(item -> (DstChannelCfg) (item)).collect(Collectors.toList());
    }

    @Override
    public void loadAllDstChannelCfg(Set<String> dbPoolCashNode, List<DstChannelCfg> dstChannelCfgList) throws Exception {
        for (String dbCaseNodeName : dbPoolCashNode) {
            String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbCaseNodeName, AppParamConstant.OLTP_POOL_NAME);
            DbsUtil dbsUtil = new DbsUtil(dbPoolName);
            String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "loadAllDstChannelCfg");
            List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, null, DstChannelCfg.class);
            if (objectList.isEmpty()) {
                rglog.warn("获取数据库连接池【{}】全量通道配置信息失败!", dbPoolName);
            } else {
                dstChannelCfgList.addAll(objectList.stream().map(item -> (DstChannelCfg) (item)).collect(Collectors.toList()));
            }
        }
    }
}
